<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use Illuminate\Support\Facades\Hash;
class IndexController extends Controller{
	public function __construct()
    {
        $this->middleware('admin');
    }
    public function index()
    {
		return view('admin.index.index');
    }
    public function console()
    {
        $todaytime=strtotime(date('Y-m-d',time()));
        $historycount = DB::table('users')->count();
        $todaycount = DB::table('users')->where('time','>',"{$todaytime}")->count();
        return view('admin.index.console',compact('historycount','todaycount'));
    }

    // 原图上传
    public function upload(){
        $file=$_FILES['file'];
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777,true);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777,true);
        }
        chmod($imgpath.$imgsubpath,0777);
        move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
        $data = [
            'code' => 0,
            'msg'   => '成功',
            'data'  => "/uploads/images/".$imgsubpath.$file['name']
        ];
        return response()->json($data);
    }
    // 原图上传（富文本）
    public function uploadedit(){
        $file=$_FILES['file'];
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath)){
            mkdir($imgpath,0777,true);
        }
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777,true);
        }
        chmod($imgpath.$imgsubpath,0777);
        move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
        // $src=array('src'=>"/uploads/images/".$imgsubpath.$file['name']);
        // $json = '{"src":'."/uploads/images/".$imgsubpath.$file['name'].'}';
        // $obj = json_decode($json);

        $obj = (object)null;
        $obj->src="/uploads/images/".$imgsubpath.$file['name'];
        $data = [
            'code' => 0,
            'msg'   => '成功',
            'data'=> $obj
        ];
        return response()->json($data);
    }
    //压缩图片并上传
    public function uploads(){
        header("Content-type: image/jpeg");
        $files=$_FILES['file'];
        $file = $files['tmp_name']; 
        $percent = 1; //图片压缩比 
        list($width, $height) = getimagesize($file); //获取原图尺寸 
        //缩放尺寸 
        $newwidth = $width * $percent; 
        $newheight = $height * $percent; 
        if(getimagesize($file)[2]==2){
           $src_im = imagecreatefromjpeg($file);
        }elseif(getimagesize($file)[2]==3){
            $src_im = imagecreatefrompng($file);
        }
        $dst_im = imagecreatetruecolor($newwidth, $newheight); 
        imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
        $imgpath="../public/uploads/images/";
        $imgsubpath=date('Ymd',time())."/";
        if(!file_exists($imgpath.$imgsubpath)){
            mkdir($imgpath.$imgsubpath,0777);
        }
        chmod($imgpath.$imgsubpath,0777);
        imagejpeg($dst_im,$imgpath.$imgsubpath.$files['name']); //输出压缩后的图片 
        imagedestroy($dst_im); 
        imagedestroy($src_im);
        $data = [
            'code' => 0,
            'msg'   => '成功',
            'data'  => "/uploads/images/".$imgsubpath.$file['name']
        ];
        return response()->json($data);
    }
    //七牛云上传
    // use Qiniu\Auth;
    // use Qiniu\Storage\UploadManager;
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
}